package com.example.pei.ithinklearnt.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.utils.ToastUtil;

import java.util.List;
import java.util.Random;

public class UserSkillAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private String[] weakSkills;
    private String[] strongSkills;
    private String[] allSkills;

    private int count=0;

    private boolean isCurrentUser;
    private int sex;  //当前展示者的性别（不是用户自己的话，才会有用

    private UserSkillListener userSkillListener;

    private Random mRandom;
    private String[] colors={"#96dc5a","#feb577","#bb9ff3","#f98f75","#ff8cc4","#5accf8","#68d4a4"};

    private  final int TYPE_WEAK = 0;    //我想学标签
    private  final int TYPE_STRONG = 1;  //我会标签
    private  final int TYPE_SKILL = 2;   //技能标签
    private  final int TYPE_NULL = 3;    //空技能提示标签


    public UserSkillAdapter(Context context,boolean isCurrentUser,int sex,UserSkillListener userSkillListener) {
        this.context=context;
        weakSkills=new String[0];
        strongSkills=new String[0];
        mRandom=new Random();
        this.userSkillListener=userSkillListener;
        this.isCurrentUser=isCurrentUser;
        this.sex=sex;
    }

    public void setSkills(String[] weaks,String[] strongs){
        count=0;
        if(weaks!=null) {
            this.weakSkills = weaks;
        }
        if(strongs!=null){
            this.strongSkills=strongs;
        }
        //将两个数组合并成一个
        allSkills = new String[weakSkills.length+strongSkills.length];
        System.arraycopy(weakSkills, 0, allSkills, 0, weakSkills.length);
        System.arraycopy(strongSkills, 0, allSkills, weakSkills.length, strongSkills.length);
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)){
            case TYPE_NULL:{
                break;
            }
            case TYPE_SKILL:{
                ((SkillViewHolder)holder).textViewSkill.setText(allSkills[count++]);
                if(((SkillViewHolder) holder).textViewSkill.getTag()==null){
                    int color = Color.parseColor(colors[mRandom.nextInt(colors.length)]);
                    ((SkillViewHolder) holder).textViewSkill.setTag(color);
                }
                ((SkillViewHolder) holder).textViewSkill.setTextColor((int)((SkillViewHolder) holder).textViewSkill.getTag());
                ((SkillViewHolder) holder).viewLine.setBackgroundColor((int)((SkillViewHolder) holder).textViewSkill.getTag());
                if(count>=allSkills.length){
                    count=0;
                }
                break;
            }
            case TYPE_STRONG:{
                ((StrongViewHolder) holder).buttonICan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        userSkillListener.chooseStrongSkills();
                    }
                });
                if(!isCurrentUser){
                    ((StrongViewHolder) holder).buttonICan.setVisibility(View.GONE);
                    ((StrongViewHolder) holder).textViewICan.setText((sex==1?"他":"她")+"会");
                }else{
                    ((StrongViewHolder) holder).buttonICan.setVisibility(View.VISIBLE);
                    ((StrongViewHolder) holder).textViewICan.setText("我会");
                }
                break;
            }
            case TYPE_WEAK:{
                ((WeakViewHolder)holder).buttonIWant.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        userSkillListener.chooseWeakSkills();
                    }
                });
                if(!isCurrentUser){
                    ((WeakViewHolder) holder).buttonIWant.setVisibility(View.GONE);
                    ((WeakViewHolder) holder).textViewIWant.setText((sex==1?"他":"她")+"想学");
                }else{
                    ((WeakViewHolder) holder).buttonIWant.setVisibility(View.VISIBLE);
                    ((WeakViewHolder) holder).textViewIWant.setText("我想学");
                }
                break;
            }
        }
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView;
        switch (viewType){
            case TYPE_NULL:{
                convertView = LayoutInflater.from(context).inflate(R.layout.noskillitem, parent, false);
                return new NoViewHolder(convertView);

            }
            case TYPE_SKILL:{
                convertView = LayoutInflater.from(context).inflate(R.layout.userskillview, parent, false);
                return new SkillViewHolder(convertView);

            }
            case TYPE_STRONG:{
                convertView = LayoutInflater.from(context).inflate(R.layout.ican, parent, false);
                return new StrongViewHolder(convertView);

            }
            case TYPE_WEAK:{
                convertView = LayoutInflater.from(context).inflate(R.layout.iwantlearn, parent, false);
                return new WeakViewHolder(convertView);

            }
        }
        return null;
    }


    @Override
    public int getItemCount() {
        //肯定有两个技能类型标签
        //如果两种技能都为空的话，还要再加两个空技能提示标签
        int count=4+weakSkills.length+strongSkills.length;
        if(weakSkills.length!=0){
            count--;
        }
        if(strongSkills.length!=0){
            count--;
        }
        return count;
    }
    @Override
    public int getItemViewType(int position) {

        if (position == 0) {                                             //第一个一定是我想学标签
            return TYPE_WEAK;
        }
        if(weakSkills.length==0){                                      //如果 没有我想学技能
            if(position==1){
                return TYPE_NULL;                 //第二个一定是空技能提示
            }
            if(position==2){
                return TYPE_STRONG;              //第三个一定是我会标签
            }
        }else if(position==weakSkills.length+1){                      //那么 我想学有技能
            return TYPE_STRONG;
        }
        if (strongSkills.length==0) {                                   //如果 没有我会技能
            if(weakSkills.length==0){
                if(position==3){  //则是我会 的空技能提示标签
                    return TYPE_NULL;
                }
            }else if(position==weakSkills.length+2){  //则是我会 的空技能提示标签
                return TYPE_NULL;
            }
        }
        return TYPE_SKILL;

    }


    public class SkillViewHolder extends RecyclerView.ViewHolder{
        View viewLine;
        TextView textViewSkill;
        public SkillViewHolder(View itemView) {
            super(itemView);
            viewLine=itemView.findViewById(R.id.view_color);
            textViewSkill=itemView.findViewById(R.id.tv_skill);
        }
    }
    public class StrongViewHolder extends RecyclerView.ViewHolder{
        TextView textViewICan;
        Button buttonICan;
        public StrongViewHolder(View itemView) {
            super(itemView);
            buttonICan=itemView.findViewById(R.id.button_ican);
            textViewICan=itemView.findViewById(R.id.tv_ican);
        }
    }
    public class WeakViewHolder extends RecyclerView.ViewHolder{
        TextView textViewIWant;
        Button buttonIWant;
        public WeakViewHolder(View itemView) {
            super(itemView);
            buttonIWant=itemView.findViewById(R.id.button_iwantlearn);
            textViewIWant=itemView.findViewById(R.id.tv_iwantlearn);
        }
    }
    public class NoViewHolder extends RecyclerView.ViewHolder{
        public NoViewHolder(View itemView) {
            super(itemView);
        }
    }


    public interface UserSkillListener{
        void chooseWeakSkills();
        void chooseStrongSkills();

    }
}
